package com.pingan.haofang.searchcloud.monitor.service.impl;

import com.pingan.haofang.searchcloud.index.service.IndexService;
import com.pingan.haofang.searchcloud.monitor.dto.DashboardIndexAggregationDTO;
import com.pingan.haofang.searchcloud.monitor.dto.DashboardIndexTopQueryResult;
import com.pingan.haofang.searchcloud.monitor.dto.DashboardResourceDTO;
import com.pingan.haofang.searchcloud.monitor.dto.SearchcloudStatusInfoDTO;
import com.pingan.haofang.searchcloud.monitor.dto.SearchcloudStatusResultDTO;
import com.pingan.haofang.searchcloud.monitor.service.DashboardIndexSummaryInfoService;
import com.pingan.haofang.searchcloud.monitor.service.DashboardResourceInfoService;
import com.pingan.haofang.searchcloud.monitor.service.SearchcloudMonitorService;
import com.pingan.haofang.searchcloud.user.service.SearchProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Optional;

/**
 * @author EX-LIUXIANWEN001
 * @date 2018/10/31
 * @since 1.0.0
 */
@Service
public class SearchcloudMonitorServiceImpl implements SearchcloudMonitorService {

    @Autowired
    private DashboardResourceInfoService dashboardResourceInfoService;

/*    @Autowired
    private DashboardApiSummaryInfoService dashboardApiSummaryInfoService;*/

    @Autowired
    private DashboardIndexSummaryInfoService dashboardIndexSummaryInfoService;

    @Autowired
    private SearchProjectService searchProjectService;

    @Autowired
    private IndexService indexService;

    @Override
    public SearchcloudStatusResultDTO querySearchcloudStatus(Date fromTime, Date toTime) {

        SearchcloudStatusResultDTO searchcloudStatusResultDTO = new SearchcloudStatusResultDTO();

        SearchcloudStatusInfoDTO searchcloudStatusInfoDTO = new SearchcloudStatusInfoDTO();

        getCountProjectAndIndex(searchcloudStatusInfoDTO);

        getDashboardResourceInfo(searchcloudStatusInfoDTO);

//        getApiStatusInfo(searchcloudStatusInfoDTO, searchcloudStatusResultDTO, fromTime, toTime);

        getIndexSummaryInfo(searchcloudStatusInfoDTO, searchcloudStatusResultDTO, fromTime, toTime);

        searchcloudStatusResultDTO.setSearchcloudStatusInfoDTO(searchcloudStatusInfoDTO);

        return searchcloudStatusResultDTO;
    }

    /*
     * 硬件信息获取
     * 
     * @param searchcloudStatusInfoDTO
     */
    private void getDashboardResourceInfo(SearchcloudStatusInfoDTO searchcloudStatusInfoDTO) {

        DashboardResourceDTO dashboardResourceDTO = dashboardResourceInfoService.queryDashboardResource();
        // 健康率 硬件使用率获取
        if (null != dashboardResourceDTO.getMapDiskOccupancy()) {
            searchcloudStatusInfoDTO.setMapDiskOccupancy(dashboardResourceDTO.getMapDiskOccupancy());
        }

        if (null != dashboardResourceDTO.getMapHealthRate()) {
            searchcloudStatusInfoDTO.setMapHealthRate(dashboardResourceDTO.getMapHealthRate());
        }
    }

    /*
     * 获取api相关调用信息
     * 
     * @param searchcloudStatusInfoDTO
     */
/*     private void getApiStatusInfo(SearchcloudStatusInfoDTO searchcloudStatusInfoDTO,
            SearchcloudStatusResultDTO searchcloudStatusResultDTO, Date fromTime, Date toTime) {

        // 获取api相关调用信息
       ApiStatusDTO apiStatusDTO = dashboardApiSummaryInfoService.queryApiStatus(fromTime, toTime);

        if (null != apiStatusDTO.getCount_4XX()) {
            searchcloudStatusInfoDTO.setCount_4XX(apiStatusDTO.getCount_4XX());
        }
        if (null != apiStatusDTO.getCount_5XX()) {
            searchcloudStatusInfoDTO.setCount_5XX(apiStatusDTO.getCount_5XX());
        }

        searchcloudStatusInfoDTO.setAvgResponse(apiStatusDTO.getAvgResponse());

        if (null != apiStatusDTO.getTotal()) {
            searchcloudStatusInfoDTO.setTotal(apiStatusDTO.getTotal());
        }

        searchcloudStatusInfoDTO.setSuccessRate(apiStatusDTO.getSuccessRate());

        // 平响top10
        searchcloudStatusResultDTO.setAvgResponseTopList(apiStatusDTO.getAvgResponseTopList());
        // 调用top10
        searchcloudStatusResultDTO.setRequestCountTopList(apiStatusDTO.getRequestCountTopList());
    }*/

    /*
     * 索引相关调用信息
     * 
     * @param searchcloudStatusInfoDTO
     */
    private void getIndexSummaryInfo(SearchcloudStatusInfoDTO searchcloudStatusInfoDTO,
            SearchcloudStatusResultDTO searchcloudStatusResultDTO, Date fromTime, Date toTime) {

        DashboardIndexTopQueryResult dashboardIndexTopQueryResult =
                dashboardIndexSummaryInfoService.indexTopView(fromTime, toTime);

        DashboardIndexAggregationDTO dashboardIndexAggregationDTO =
                dashboardIndexTopQueryResult.getDashboardIndexAggregationDTO();

        // 索引成功率
        if (null != dashboardIndexAggregationDTO.getIndexSuccessRate()
                && !Double.isNaN(dashboardIndexAggregationDTO.getIndexSuccessRate())) {
            searchcloudStatusInfoDTO.setIndexSuccessRate(dashboardIndexAggregationDTO.getIndexSuccessRate());
        }

        long indexCostTime = Optional.ofNullable(dashboardIndexAggregationDTO)
                .map(DashboardIndexAggregationDTO::getIndexCostTime).orElse(0L);

        // 索引总耗时
        searchcloudStatusInfoDTO.setIndexCostTime(indexCostTime);

        // 索引耗时top10
        searchcloudStatusResultDTO.setIndexCostTop10(dashboardIndexTopQueryResult.getIndexCostTop10());
        // 索引失败top10
        searchcloudStatusResultDTO.setIndexFailTop10(dashboardIndexTopQueryResult.getIndexFailTop10());
    }

    /**
     * 获得项目量和索引量
     * @param searchcloudStatusInfoDTO
     */
    private void getCountProjectAndIndex(SearchcloudStatusInfoDTO searchcloudStatusInfoDTO) {

        int countProject = searchProjectService.countProject();

        searchcloudStatusInfoDTO.setCountProject(countProject);

        int countIndex = indexService.countIndex();

        searchcloudStatusInfoDTO.setCountIndex(countIndex);

    }

}
